<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/novel_channelList' }">频道列表</el-breadcrumb-item>
            <el-breadcrumb-item>轮播图列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <div class="el-card-button">
                <el-button type="primary" size="small" @click="addRecharge">添加</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%">
                <el-table-column
                        prop="list_order"
                        label="排序">
                </el-table-column>
                <el-table-column
                        label="缩略图">
                    <template slot-scope="scope">
                        <img :src="scope.row.image" alt="" width="30" height="30" class="img">
                    </template>
                </el-table-column>
                <el-table-column
                        :width="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'150':''"
                        label="小说（标题）">
                    <template slot-scope="scope">
                        <img :src="scope.row.animeInfo.coverpic" alt="" width="30" height="30" class="img">
                        {{scope.row.animeInfo.title}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="状态">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.status" active-value="1" inactive-value="0"
                                @change="changeClick(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>

                <el-table-column
                        :width="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'150':''"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button type="warning" plain size="small" @click="edit(scope.row.id)">修改</el-button>
                        <el-button type="danger" plain size="small" @click="remove(scope.row.id)">删除</el-button>
                    </template>

                </el-table-column>
            </el-table>
            <div class="el-pagination">
                <el-pagination
                        background
                        @size-change="changeSize"
                        @current-change="changeCurrent"
                        :current-page="params.pagenum"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="params.pagesize"
                        :total="params.totals"
                        :layout="this.$store.state.paginationLayout">
                </el-pagination>
            </div>
            <el-dialog :title="fromTitle" :width="this.$store.state.dialogWidth" :visible.sync="visible"
                       @close='loading=false'>
                <el-row :gutter="20">
                    <el-col :span="this.$store.state.elColNum">
                        <el-form ref="form" :label-width="this.$store.state.labelWidth" size="small"
                                 v-loading="loading">

                            <el-form-item label="排序">
                                <el-input v-model="form.list_order"></el-input>
                            </el-form-item>
                            <el-form-item label="标题">
                                <el-input v-model="form.title"></el-input>
                            </el-form-item>
                            <!--                            <el-form-item label="banner链接">-->
                            <!--                                <el-input v-model="form.url"></el-input>-->
                            <!--                            </el-form-item>-->
                            <el-form-item label="小说">
                                <el-autocomplete
                                        class="inline-input"
                                        style="width: 100%"
                                        v-model="form.name"
                                        :fetch-suggestions="querySearch"
                                        placeholder="请输入小说名"
                                        @select="handleSelect"
                                ></el-autocomplete>
                            </el-form-item>
                            <!--                            <el-form-item label="时间">-->
                            <!--                                <el-date-picker-->
                            <!--                                        v-model="form.timer"-->
                            <!--                                        type="daterange"-->
                            <!--                                        value-format="yyyy-MM-dd"-->
                            <!--                                        start-placeholder="开始日期"-->
                            <!--                                        end-placeholder="结束日期">-->
                            <!--                                </el-date-picker>-->
                            <!--                            </el-form-item>-->
                            <el-form-item label="开始时间">
                                <el-date-picker
                                        v-model="form.start_time"
                                        type="date"
                                        style="width: 100%"
                                        placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="结束时间">
                                <el-date-picker
                                        v-model="form.end_time"
                                        type="date"
                                        style="width: 100%"
                                        placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="缩略图">
                                <el-upload
                                        class="avatar-uploader"
                                        action="upDataUrl"
                                        :show-file-list="false"
                                        :http-request="myUpload"
                                        :before-upload="beforeAvatarUpload1">
                                    <img v-if="form.image" :src="form.image" class="avatar">
                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                </el-upload>
                            </el-form-item>
                            <el-form-item label="视频">
                                <el-upload
                                        class="upload-dem"
                                        action="upDataUrl"
                                        :show-file-list="false"
                                        :http-request="myUpVideo"
                                        :before-upload="beforeAvatarUpload2">
                                    <video :src="form.video" controls="controls" style="width: 100%"
                                           v-if="form.video"></video>
                                    <el-button size="small" type="primary">点击上传</el-button>
                                </el-upload>
                            </el-form-item>
                            <el-form-item label="状态">
                                <el-select v-model="form.status" placeholder="状态">
                                    <el-option label="启用" value="1"></el-option>
                                    <el-option label="禁用" value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
                <span slot="footer" class="dialog-footer">
                <el-button @click="visible=false" size="small">取 消</el-button>
                <el-button type="primary" @click="dialogAffirm" size="small">确 定</el-button></span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
  import {uploadimg, formatDate, isRealNum} from '../../../utils/common'

  export default {
    name: "Comic_Release",
    data() {
      return {
        params: {
          pagenum: 1,
          pagesize: 10,
          totals: 0
        },
        tableData: [],
        form: {
          title: '',
          list_order: '',
          start_time: '',
          end_time: '',
          url: '',
          video: '',
          image: '',
          status: '',
          timer: [],
          name: '',
          anid: '',
        },
        fromTitle: '新增',
        visible: false,
        id: '',
        channel_id: '',
        fileList: [],
        loading: false
      }
    },
    created() {
      this.channel_id = this.$route.query.channel_id;
      this.getList();
      this.loadAll();
    },
    methods: {
      changeClick(e) {
        this.$axios.put('admin/channel_ad/' + e.id, {
          status: e.status
        }).then((res) => {
          this.$message.success('成功');
          this.getList();
          this.visible = false;
        });
      },
      addRecharge() {
        this.fromTitle = '新增';
        this.id = '';
        this.fileList = [];
        this.form = {
          title: '',
          list_order: '',
          start_time: '',
          end_time: '',
          url: '',
          video: '',
          image: '',
          status: '',
          timer: [],
          name: '',
          anid: '',
        };
        this.visible = true;
      },
      getList() {
        let data = {
          page: this.params.pagenum,
          page_size: this.params.pagesize,
          channel_id: this.channel_id
        };
        this.$axios.post("admin/channel_ad/index", data).then((res) => {
          this.params.totals = parseInt(res.data.count);
          this.tableData = res.data.list;
        });
      },
      // 每页显示多少条
      changeSize(val) {
        this.params.pagesize = val;
        this.getList();
      },
      // 当前显示的页数
      changeCurrent(val) {
        this.params.pagenum = val;
        this.getList()
      },
      // 编辑
      edit(id) {
        this.id = id;
        this.fromTitle = '编辑';
        this.$axios.get('admin/channel_ad/' + id, {}).then((res) => {
          let data = res.data;
          for (let key in data) {
            if (isRealNum(data[key])) {
              data[key] = data[key].toString()
            }
          }
          // data.timer = [formatDate(data.start_time), formatDate(data.end_time)];
          data.start_time = formatDate(data.start_time);
          data.end_time = formatDate(data.end_time);
          this.restaurants.map((num) => {
            if (num.id == data.anid) {
              data.name = num.title;
            }
          });
          this.form = data;
          this.visible = true;
        });
      },
      // 确认提交添加或编辑用户
      dialogAffirm() {
        // this.form.start_time = this.form.timer[0];
        // this.form.end_time = this.form.timer[1];
        if (this.id) {
          this.$axios.put('admin/channel_ad/' + this.id, this.form).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        } else {
          // 添加
          this.form = Object.assign(this.form, {channel_id: this.channel_id})
          this.$axios.post('/admin/channel_ad', this.form).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        }
      },
      remove(id) {
        this.$confirm('确定要进行操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.del('admin/channel_ad/' + id, '').then((res) => {
            this.$message.success('删除成功!');
            this.getList();
          });
        }).catch(() => {

        });
      },

      beforeAvatarUpload1(file) {
        var suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
        let arr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'pdf', 'JPG', 'JPEG', 'PNG', 'GIF', 'BMP', 'PDF']
        if (arr.indexOf(suffix) === -1) {
          this.$message.error('上传图片格式有误');
          return false
        }
      },
      beforeAvatarUpload2(file) {
        let suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
        let arr = ['mp4', 'flv', 'avi', 'MP4', 'FLV', 'AVI'];
        if (arr.indexOf(suffix) === -1) {
          this.$message.error('上传视频格式有误');
          return false
        }
      },
      // 上传图片文件
      myUpload(content) {
        uploadimg(content).then(res => {
          this.form.image = res;
        })
      },
      // 上传视频
      myUpVideo(content) {
        this.loading = true;
        uploadimg(content).then(res => {
          this.form.video = res;
          this.loading = false;
        })
      },
      loadAll() {
        this.$axios.post("admin/novel/getAll", '').then((res) => {
          this.restaurants = res.data;
          this.restaurants.map((num) => {
            num.value = num.title;
          })
        });
      },
      querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelect(item) {
        this.form.anid = item.id;
      },
    }
  }
</script>

<style scoped>
    .img {
        vertical-align: middle;
    }
</style>